package com.mimiwang.oss.controller;

import com.google.common.base.Strings;
import com.mimiwang.common.utils.ResponseUtil;
import com.mimiwang.oss.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @AUTHOR:游李滨
 * @DATE:2023/4/10 14:39
 */

@RestController()
public class FileUploadController {

    @Autowired
    FileUploadService fileUploadService;


    @PostMapping("/local")
    public String upload(MultipartFile file){
        if(file==null){
            System.out.println("文件为空");
            ResponseUtil.errorJSONData("文件为空");
        }

        String url = fileUploadService.localStore(file);

        if(url==null){
            return ResponseUtil.errorJSONData("非法格式文件!");
        }
        return ResponseUtil.successJSONData("上传成功",url);
    }
    @PostMapping("/local/files")
    public String upload(MultipartFile[] files){
        if(files==null){
            System.out.println("文件为空");
            ResponseUtil.errorJSONData("文件为空");
        }

        List<String> urlList = fileUploadService.localStoreFiles(files);
        List<ResponseUtil.BaseUtil> ans = new ArrayList<>();
        for(String url:urlList){
            if (Strings.isNullOrEmpty(url)){
                ans.add(ResponseUtil.errorJSONObject("上传失败"));
            }else{
                ans.add(ResponseUtil.successJSONObject("上传成功",url));
            }
        }

        return ResponseUtil.successJSONData("上传成功",ans);
    }

}
